package com.infore.siteManagement.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * session工具类
 * 
 * @author zwt
 *
 */
public class SessionUtil {
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getRequest();
    }

    // 获取session
    public static HttpSession getSession() {
        return getRequest().getSession(false);
    }
    
    /**
     * 获取response
     * 
     * @author 
     * @return HttpServletResponse
     * @return
     * @date 
     */
   /* public static HttpServletResponse getResponse() {
        ServletWebRequest requestAttributes = (ServletWebRequest) RequestContextHolder
                .getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getResponse();
    }
    */

    // 获取session属性
    public static Object getSessionAttribute(String name) {
        HttpServletRequest request = getRequest();
        return request == null ? null : request.getSession().getAttribute(name);
    }

    public static String param(String paramName, String defaultValue) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        String value = request.getParameter(paramName);

        if (!StringUtils.isNotBlank(value)) {
            value = defaultValue;
        }
        request.setAttribute(paramName, value);
        return value;
    }

    public static String stringParam(String paramName, String defaultValue) {
        return param(paramName, defaultValue).trim();
    }

    public static int intParam(String paramName, int defaultValue) {
        return Integer.parseInt(stringParam(paramName, defaultValue + ""));
    }
}
